package com.tgy.MyFlowable.workflow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctsi.core.common.exception.MarsException;
import com.ctsi.core.common.util.$;
import com.ctsi.core.common.util.StringUtil;
import com.ctsi.core.database.entity.Search;
import com.ctsi.core.database.util.PageUtil;
import com.ctsi.core.workflow.enums.FormNodeTypeEnum;
import com.ctsi.core.workflow.enums.FormResourceTypeEnum;
import com.ctsi.workflow.dto.FlwFormDTO;
import com.ctsi.workflow.dto.FlwFormResourceDTO;
import com.ctsi.workflow.entity.FlwForm;
import com.ctsi.workflow.entity.FlwFormResource;
import com.ctsi.workflow.enums.FlwFormExceptionEnum;
import com.ctsi.workflow.mapper.FlwFormMapper;
import com.ctsi.workflow.service.IFlwFormResourceService;
import com.ctsi.workflow.service.IFlwFormService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 流程表单配置表 服务实现类
 *
 * @version 1.0
 * @author: wang xiao xiang
 * @date: 2021/8/8 19:36
 */
@Service
@RequiredArgsConstructor
public class FlwFormServiceImpl extends ServiceImpl<FlwFormMapper, FlwForm> implements IFlwFormService {

    private final IFlwFormResourceService flwFormResourceService;

    @Override
    public IPage<FlwForm> listPage(Search search) {
        LambdaQueryWrapper<FlwForm> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtil.isNotBlank(search.getKeyword())) {
            queryWrapper.like(FlwForm::getId, search.getKeyword());
        }
        queryWrapper.orderByDesc(FlwForm::getCreateTime);
        return this.baseMapper.selectPage(PageUtil.getPage(search), queryWrapper);
    }

    @Override
    public void delete(String id) {
        this.remove(Wrappers.<FlwForm>lambdaQuery().eq(FlwForm::getProcessDefinitionId, id));
    }

    @Override
    public FlwForm detail(FlwFormDTO flwFormDTO) {
        return this.queryFlwForm(flwFormDTO);
    }

    @Override
    public String startFormData(FlwFormDTO flwFormDTO) {
        FlwForm flwForm = this.getOne(Wrappers.<FlwForm>lambdaQuery()
                .eq(FlwForm::getProcessDefinitionId, flwFormDTO.getProcessDefinitionId())
                .eq(FlwForm::getNodeType, FormNodeTypeEnum.START.getCode()));
        if ($.isNull(flwForm)) {
            throw new MarsException(FlwFormExceptionEnum.FORM_START_NOT_EXIST);
        }
        return getFlwFormResource($.toStr(flwForm.getFormId()));
    }

    @Override
    public String getGlobalFormData(FlwFormDTO flwFormDTO) {
        FlwForm flwForm = this.getOne(Wrappers.<FlwForm>lambdaQuery()
                .eq(FlwForm::getProcessDefinitionId, flwFormDTO.getProcessDefinitionId())
                .eq(FlwForm::getNodeType, FormNodeTypeEnum.GLOBAL.getCode()));
        if ($.isNull(flwForm)) {
            throw new MarsException(FlwFormExceptionEnum.FORM_GLOBAL_NOT_EXIST);
        }
        return getFlwFormResource($.toStr(flwForm.getFormId()));
    }

    @Override
    public String getTaskFormData(FlwFormDTO flwFormDTO) {
        FlwForm flwForm = this.getOne(Wrappers.<FlwForm>lambdaQuery()
                .eq(FlwForm::getProcessDefinitionId, flwFormDTO.getProcessDefinitionId())
                .eq(FlwForm::getNodeType, FormNodeTypeEnum.NODE.getCode())
                .eq(FlwForm::getActId,flwFormDTO.getActId()));
        if ($.isNull(flwForm)) {
            //流程节点无表单则使用全局表单
            return getGlobalFormData(flwFormDTO);
        }
        return getFlwFormResource($.toStr(flwForm.getFormId()));
    }

    /**
     * 获取表单
     *
     * @param flwFormDTO 请求参数
     * @return
     */
    private FlwForm queryFlwForm(FlwFormDTO flwFormDTO) {
        FlwForm flwForm = this.getById(flwFormDTO.getId());
        if ($.isNull(flwForm)) {
            throw new MarsException(FlwFormExceptionEnum.FORM_NOT_EXIST);
        }
        return flwForm;
    }

    /**
     * 根据表单参数查询表单资源
     *
     * @param formId 表单ID
     * @return 表单资源
     */
    private String getFlwFormResource(String formId) {
        FlwFormResourceDTO flwFormResourceDTO = new FlwFormResourceDTO();
        flwFormResourceDTO.setId(formId);
        FlwFormResource flwFormResource = flwFormResourceService.detail(flwFormResourceDTO);
        //如果是自行开发则返回url
        Integer type = flwFormResource.getType();
        if (FormResourceTypeEnum.DEV.getCode().equals(type)) {
            return flwFormResource.getFormUrl();
        } else {
            //否则返回表单json
            return flwFormResource.getFormJson();
        }
    }
}
